package Demo7.Obj;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ObjectStream {

    static List<Student> list = new ArrayList<>();
    static List<Student> lists = new ArrayList<>();

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        read();
        System.out.println("需要继续添加吗");
        while (input.next().equals("y")){
            System.out.println("输入ID和名字");
            Student s = new Student(input.nextInt(),input.next());
            list.add(s);
        }
        write(list);
        read();
    }
    public static void write(List<Student> list){
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("f:/TEST/hello.txt"));
            for (Student student : list) {
                lists.add(student);
            }
            oos.writeObject(lists);
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void read(){
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("f:/TEST/hello.txt"));
            lists =(List<Student>) ois.readObject();
            for (Student student : lists) {
                System.out.println(student.getId()+"----"+student.getName());
            }
            ois.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
